﻿
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Xml.Linq;
using CSM.Controllers;
using CSM.Models;
using System.Collections.Generic;

namespace CSM.Models
{
    public class CSMPageBase : System.Web.UI.Page
    {
        private CustomDataContext db = new CustomDataContext();


        protected void Page_PreInit(object sender, EventArgs e)
        {
            string requestFile = Request.ServerVariables["PATH_INFO"].ToString().ToLower();
            var menu = GetMenu(requestFile);

            if (Session[SystemVars.sysSessionUserAuths] == null)
            {
                Response.Write(string.Format(@"<meta http-equiv=""refresh"" target=""_top"" content=""0;url=/Login.aspx?redir={0}"">", requestFile));
                Response.End();
            }

            var b = ((List<int>)Session[SystemVars.sysSessionUserAuths]).Contains(menu.MenuID);
            if (!b && menu.MenuID != 0)
            {
                throw new Exception("无权访问!");
            }
        }

        private TCsm_JS_Menu GetMenu(string pathfile)
        {
            if (db.TCsm_JS_Menu.Count(m => m.URL == pathfile) == 0)
            {
                throw new Exception("菜单数据库中没有对应的权限!");
            }
            var menu = (from m in db.TCsm_JS_Menu
                        where m.URL == pathfile
                        select m).Single();
            return menu;
        }
    }
}
